/**
 * 
 */
package com.gkwave.action.service;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gkwave.module.bean.MessageBean;
import com.gkwave.module.dao.TradeProductDAO;

/**
 * @author Administrator
 *
 */
public class TradeProductBuyService extends BaseService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3951778062176371228L;

	/**
	 * 
	 */
	public TradeProductBuyService() {
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see com.gkwave.action.service.BaseService#procesRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public void procesRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
    	String nrBidAmount = request.getParameter("nrBidAmount");
    	String idUser = request.getParameter("idUser");
    	String idTradeProduct = request.getParameter("idTradeProduct");
    	String dtTradeProductOrder = request.getParameter("dtTradeProductOrder");
    	MessageBean bean = null;
    	if(nrBidAmount!=null && idUser!=null && idTradeProduct!=null && dtTradeProductOrder!=null){
    		Double nr_bid_amount = Double.parseDouble(nrBidAmount);
    		Integer id_user = Integer.parseInt(idUser);
    		Integer id_trade_product = Integer.parseInt(idTradeProduct);
    		
    		TradeProductDAO dao = new TradeProductDAO();
    		String query = "INSERT INTO h_trade_product (id_trade_product,id_user,nr_trade_product_price,dt_trade_product_order) VALUES ("
				+ "'" +id_trade_product+ "'"
				+ ","
				+ "'" +id_user+ "'"
				+ ","
				+ "'" +nr_bid_amount+ "'"
				+ ",'" +dtTradeProductOrder+ "')";
    		
    		boolean return_value = dao.setBidTradeProduct(query);
    		
    		if(return_value){
    			Integer maxId = dao.getMaxHTradeProductId();
    			query = "UPDATE trade_product SET id_h_trade_product="+maxId+",nr_trade_high_price="+nr_bid_amount+" WHERE id_trade_product='" +id_trade_product+"'";
    			return_value = dao.setBidTradeProduct(query);
    			bean = new MessageBean("Success" , "Bit amount entered successfully");
    			xmlResponse = bean.marsel(bean);
    		} else{
    			bean = new MessageBean("Error" , "Error!!!");	
    		}
    	} else{
    		bean = new MessageBean("Error" , "Empty input parameter > [ nrBidAmount:"+nrBidAmount+",idUser:"+idUser+",idTradeProduct:"+idTradeProduct+",dtTradeProductOrder:"+dtTradeProductOrder+" ]");	
    	}
    	xmlResponse = bean.marsel(bean);
    	System.out.println(xmlResponse);
    	out=response.getWriter();
    	out.println(xmlResponse);
	}

}
